package fr.kotton.myorganizer.task.fragment;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.LinkedList;

import fr.kotton.myorganizer.R;
import fr.kotton.myorganizer.bean.Task;
import fr.kotton.myorganizer.task.interfaces.TasksQueryCompleteListener;
import fr.kotton.myorganizer.utils.Consts;
import fr.kotton.myorganizer.utils.sql.DatabaseReader;
import fr.kotton.myorganizer.utils.sql.DbToBeanConverter;
import fr.kotton.myorganizer.utils.sql.SqlRawQuery;

public class ViewTaskFragment extends Fragment {

    TasksQueryCompleteListener mTasksDbQueryCompletedListener;
    private Integer mTaskId;
    private Task mTask;
    private DatabaseReader mDatabaseReader;

    public ViewTaskFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_view_task, container, false);

        setHasOptionsMenu(true);

        Bundle lArgs = this.getArguments();
        mTaskId = lArgs.getInt(Consts.INTENT_REF_TASK_ID, 0);

        TextView lTextView = (TextView) rootView.findViewById(R.id.fragment_view_task_task_id);
        lTextView.setText("Tâche id : " + mTaskId);

        // Configuration des callbacks appelés lorsqu'une requête DB est terminée
        mTasksDbQueryCompletedListener = new TasksQueryCompleteListener() {

            @Override
            public void onTasksReadQueryComplete(Cursor pCursor) {
                parseTasksReadQueryResult(pCursor);
            }

            @Override
            public void onTasksDeleteQueryComplete() {
            }
        };

        // Requête pour récupérer la liste des tâches
        SqlRawQuery lSqlRawQuery = new SqlRawQuery();
        lSqlRawQuery.setQuery("SELECT task.task_id, Title, endDate, task_to_task_group.task_id as task_id_ref, task_to_task_group.taskgroup_id, task_group.name, task_group.color FROM task LEFT JOIN task_to_task_group ON task_id_ref = task.task_id LEFT JOIN task_group ON task_to_task_group.taskgroup_id = task_group.taskgroup_id WHERE task.task_id = " + mTaskId);
        mDatabaseReader = new DatabaseReader(getActivity(), mTasksDbQueryCompletedListener);
        mDatabaseReader.execute(lSqlRawQuery);

        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.fragment_view_task_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        } else if (id == R.id.action_edit_task) {

            Bundle lBundle = new Bundle();
            lBundle.putInt(Consts.INTENT_REF_TASK_ID, mTaskId);

            EditTaskFragment lEditTaskFragment = new EditTaskFragment();
            lEditTaskFragment.setArguments(lBundle);

            getActivity().getSupportFragmentManager().beginTransaction()
                    .addToBackStack(null)
                    .replace(R.id.container, lEditTaskFragment)
                    .commit();
        }
        return super.onOptionsItemSelected(item);
    }

    public void parseTasksReadQueryResult(Cursor pCursor) {
        if (pCursor != null) {
            LinkedList<Task> lTasksList = DbToBeanConverter.DbToTasks(pCursor);
            mTask = lTasksList.get(0);

            TextView lTaskTitle = (TextView) getView().findViewById(R.id.fragment_view_task_task_title);
            TextView lTaskEndDate = (TextView) getView().findViewById(R.id.fragment_view_task_task_date);
            TextView lTaskTime = (TextView) getView().findViewById(R.id.fragment_view_task_task_time);

            lTaskTitle.setText(mTask.getTitle());
            lTaskEndDate.setText(mTask.getFormattedEndDate());
            lTaskTime.setText(mTask.getFormattedEndTime());
        }
    }
}

